package org.dromara.neutrinoproxy.client.mini;

import org.dromara.neutrinoproxy.client.core.ProxyClientService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MyWindow implements IClientDataCallback {

    /**
     * 窗口
     */
    private JFrame frame;

    /**
     * licenseKey 输入框
     */
    private JTextField licenseKeyTextArea;


    private JButton ok;

    // 窗口宽度
    public static final int WINDOW_WIDTH = 400;

    // 窗口高度
    public static final int WINDOW_HEIGHT = 250;

    private ProxyClientService proxyClientService;


    public void showWindow(){
        if(frame == null){
            initFrame();
        }
        frame.setVisible(true);
    }


    /**
     * 初始化窗口
     */
    private void initFrame(){
        frame = new JFrame("云代理客户端");
//        frame.setBounds(x, y, WINDOW_WIDTH, WINDOW_HEIGHT);
        frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        frame.setLayout(null);
        frame.setResizable(false);
        initInput(frame);

        ClientDataService.getInstance().addCallback(this);
    }

    /**
     * 添加输入框
     */
    private void initInput(Frame frame) {

        final int width = 200;
        final int height = 30;
        final int margin = 20;
        final int x = (WINDOW_WIDTH - width) / 2;
        final int y = margin;

        JLabel field = new JLabel();
        field.setBounds(x, y, width, height);
        field.setText("请输入LicenseKey");
        frame.add(field);

        final int labelX = x;
        final int labelY = y + height + margin;

        licenseKeyTextArea = new JTextField();
        licenseKeyTextArea.setBounds(labelX, labelY, width, height);
        frame.add(licenseKeyTextArea);
        if(proxyClientService != null){
            licenseKeyTextArea.setText(proxyClientService.getCurrentLicenseKey());
        }



        final int buttonX = x;
        final int buttonY = labelY + height + margin;
        final int buttonWidth = width / 2 - margin / 2;

        ok = new JButton("连接");
        ok.setBounds(buttonX, buttonY, buttonWidth, height);
        ok.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(ok.getText().equals("断开")){
//                    licenseKeyTextArea.setEnabled(true);
//                    ok.setText("连接");
                    if(proxyClientService != null){
                        proxyClientService.stop();
                    }
                } else {
                    String licenseKey = licenseKeyTextArea.getText();
                    if(licenseKey != null && !licenseKey.equals("") && proxyClientService != null){
                        proxyClientService.setCurrentLicenseKey(licenseKey);
                    }
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        frame.add(ok);
        if(proxyClientService != null){
            if(proxyClientService.connected()){
                ok.setText("断开");
                licenseKeyTextArea.setEnabled(false);
            } else {
                ok.setText("连接");
                licenseKeyTextArea.setEnabled(true);
            }
        }

        JButton cancel = new JButton("退出！");
        cancel.setBounds(buttonX + buttonWidth + margin, buttonY, buttonWidth, height);
        cancel.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new MyDialog("提示", "是否要关闭该程序?").setVisible(true);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        frame.add(cancel);


    }

    @Override
    public void onDataChanged(ClientDataService data) {
        if(data.isConnecting() || data.isConnected()){
//            ok.setEnabled(false);
            ok.setText("断开");
            licenseKeyTextArea.setEnabled(false);
        } else {
            ok.setText("连接");
            licenseKeyTextArea.setEnabled(true);
        }
    }

    public void setProxyClientService(ProxyClientService proxyClientService) {
        this.proxyClientService = proxyClientService;
    }
}
